package thread;

// 实现 Runnable 接口, 重写 run
class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("实现Runnable的接口的子线程被执行了 " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class demo2 {
    public static void main(String[] args) throws InterruptedException {
        // 1. 创建 Thread 类实例, 调用 Thread 的构造方法时将 Runnable 对象作为 target 参数
        Thread thread = new Thread(new MyRunnable());

        // 2. 启动线程
        thread.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("主线程main方法被执行了 " + i);
            Thread.sleep(1000);
        }
    }

}
